package com.jxy.bean.constant;

import com.jxy.bean.ClassFile;
import com.jxy.tool.Tool;

import java.io.DataInputStream;
import java.nio.charset.StandardCharsets;

public class Utf8Info extends ConstantPool {
    private int length;
    private byte[] bytes;

    public Utf8Info(DataInputStream dis, ClassFile cf) {
        super(ConstantPoolTag.CONSTANT_Utf8.getTag(), cf);
        //长度u2
        length = Tool.readU2(dis);
        bytes = Tool.readBytes(dis, length);
    }

    @Override
    public String toString() {
        return ConstantPoolTag.CONSTANT_Utf8.getType() + "{tag:" + tag + ", length:" + length + ", bytes=" + getString() + "}";
    }

    public byte[] getBytes() {
        return bytes;
    }

    public String getString() {
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
